package com.ruoyi.web.controller.leftover;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.system.service.ILeftoverService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 边角料控制层
 *
 * @author C3007106
 */
@Controller
@RequestMapping("/system/leftover")
public class LeftoverController extends BaseController {

    private String prefix = "leftover";

    @Autowired
    private ILeftoverService leftoverService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ISysDeptService deptService;

    @GetMapping("/view")
    public String view() {
        return prefix + "/view";
    }

    @RequiresPermissions("system:leftover:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Leftover leftover, ModelMap mmap) {
        startPage();
        List<Leftover> list = leftoverService.selectLeftoverList(leftover);
        return getDataTable(list);
    }

    @GetMapping("/query")
    public String queryView() {
        return prefix + "/query";
    }

    @RequiresPermissions("system:leftover:query")
    @PostMapping("/query")
    @ResponseBody
    public TableDataInfo query(LeftoverQuery leftoverQuery, ModelMap mmap) {
        startPage();
        List<LeftoverQuery> list = leftoverService.selectLeftoverQueryList(leftoverQuery);
        return getDataTable(list);
    }

    // 导出数据
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Leftover leftover) {
        List<Leftover> list = leftoverService.selectLeftoverList(new Leftover());
        ExcelUtil<Leftover> util = new ExcelUtil<Leftover>(Leftover.class);
        return util.exportExcel(list, "邊角料數據数据");
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:leftover:edit")
    @GetMapping("/edit/{tableId}")
    public String edit(@PathVariable("tableId") String tableId, ModelMap mmap) {
        mmap.put("leftover", leftoverService.selectLeftoverByTableId(tableId));
        mmap.put("materials", companyService.selectAllMaterial());
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("system:leftover:edit")
    @Log(title = "邊角料信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Leftover leftover) {
        return toAjax(leftoverService.updateLeftover(leftover));
    }

    @Log(title = "邊角料信息導入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:leftover:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        //更新料號材質等信息
        ExcelUtil<UpdateMes> util = new ExcelUtil<UpdateMes>(UpdateMes.class);
        List<UpdateMes> updateMesList = util.importExcel(file.getInputStream());
        leftoverService.updateMes(updateMesList);

        //有效料號
//        ExcelUtil<LeftoverIdState> util = new ExcelUtil<LeftoverIdState>(LeftoverIdState.class);
//        List<LeftoverIdState> leftoverIdStates = util.importExcel(file.getInputStream());
//        leftoverService.updateState(leftoverIdStates);
//        leftoverService.updateMaterial(leftoverIdStates);

        //導入數據
//        ExcelUtil<LeftoverImport> util = new ExcelUtil<LeftoverImport>(LeftoverImport.class);
//        List<LeftoverImport> leftoverList = util.importExcel(file.getInputStream(), 4);
////        String message = leftoverService.importLeftover(leftoverList, updateSupport, getLoginName());
//        List<LeftoverImport> list = leftoverService.handleData(leftoverList);


        return AjaxResult.success("恭喜您，数据已全部导入成功！");
    }

    /**
     * 选择边角料料号
     */
    @GetMapping(value = {"/selectLeftoverId"})
    public String selectDeptTree(ModelMap mmap) {
        mmap.put("leftovers", leftoverService.selectLeftoverIdList(null));
        return prefix + "/tree";
    }

    /**
     * 选择边角料料号
     */
    @GetMapping(value = {"/selectLeftoverId/{companyId}"})
    public String selectDeptTree(@PathVariable(value = "companyId", required = false) String companyId, ModelMap mmap) {
        mmap.put("leftovers", leftoverService.selectLeftoverIdList(companyId));
        return prefix + "/tree";
    }

    /**
     * 加载邊角料料號
     */
    @GetMapping("/data/{companyId}")
    @ResponseBody
    public List<Ztree> treeData(@PathVariable(value = "companyId", required = false) String companyId) {
        List<Ztree> ztrees = leftoverService.selectLeftoverIdTree(companyId);
        return ztrees;
    }
}
